package com.boot.down.util.redis;

import redis.clients.jedis.Jedis;

import static com.boot.down.util.redis.LockConstants.NOT_EXIST;
import static com.boot.down.util.redis.LockConstants.OK;
import static com.boot.down.util.redis.LockConstants.SECONDS;


/**
 * 加锁与解锁(释放锁) 测试二(给锁加上了过期时间 , 但是这是出现 释放锁紊乱(可能A将B的释放了),
 * 还有就是有效时间怎么保证大于业务时间来执行)
 * @author dell
 */
public class LockCase2 extends RedisLock {


	public LockCase2(Jedis jedis, String lockKey) {
		super(jedis, lockKey);
	}

	@Override
	public void lock() {
		while (true) {
			//设置锁的过期时间
			String result = jedis.set(lockKey, "value", NOT_EXIST, SECONDS,30);
			if (OK.equals(result)) {
				System.out.println(Thread.currentThread().getId() + "加锁成功!");
				break;
			}
		}
	}

	@Override
	public void unlock() {
		//解锁(释放锁)
		jedis.del(lockKey);
	}
}
